/*
# ------------------------------------------------------------------------
# JD Base Theme
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/
function switchFontSize (ckname,val){
	
	var bd = document.getElementsByTagName('body');
	if (!bd || !bd.length) return;
	bd = bd[0];
	var oldclass = 'fs'+CurrentFontSize;
	switch (val) {
		case 'inc':
			if (CurrentFontSize+1 < 7) {
				CurrentFontSize++;
			}		
		break;
		case 'dec':
			if (CurrentFontSize-1 > 0) {
				CurrentFontSize--;
			}		
		break;
		case 'reset':
		default:
			CurrentFontSize = DefaultFontSize;			
	}
	var newclass = 'fs'+CurrentFontSize;
	bd.className = bd.className.replace(new RegExp('fs.?', 'g'), '');
	bd.className = trim(bd.className);
	bd.className += (bd.className?' ':'') + newclass;
	createCookie(ckname, CurrentFontSize, 365);
}

function switchTool (ckname, val) {
	createCookie(ckname, val, 365);
	window.location.reload();
}

function cpanel_reset () {
	var args = ['font', 'screen', 'profile', 'layout_desktop', 'menu_style'];
	
	for(i=0;i<args.length;i++){
		var c_name = tmpl_name + '_' + args[i];
		if(jQuery.cookie(c_name)){
			jQuery.cookie (c_name, '', -1);
		}
	}
	
	window.location.reload();
}

function escapeRegExp(str){
	return str.replace(/([-.*+?^${}()|[\]\/\\])/g, '\\$1');
}

function cpanel_apply () {
	var elems = document.getElementById('ja-cpanel-main').getElementsByTagName ('*');
	
	var usersetting = {};
	for (i=0;i<elems.length;i++) {
		var el = elems[i]; 
	    if (el.name && (match=el.name.match(/^user_(.*)$/))) {
	        var name = match[1];	        
	        var value = '';
	        if (el.tagName.toLowerCase() == 'input' && (el.type.toLowerCase()=='radio' || el.type.toLowerCase()=='checkbox')) {
	        	if (el.checked) value = el.value;
	        } else {
	        	value = el.value;
	        }
	        if (usersetting[name]) {
	        	if (value) usersetting[name] = value + ',' + usersetting[name];
	        } else {
	        	usersetting[name] = value;
	        }
	    }
	}
	
	for (var k in usersetting) {
		name = tmpl_name + '_' + k;
		value = usersetting[k];
		createCookie(name, value, 365);
	}
	
	window.location.reload();
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


function fixHeight (els_, group1, group2) {
	
	els = jQuery(els_);
	group1 = jQuery(group1);
	group2 = jQuery(group2);
	if (!els || !group1) return;
	var height = 0;
	group1.each (function (index,el){
		height += $(el).height();
	});
	if (group2) {
		group2.each (function (i, el){
			height -= $(el).height();
		});
	}
	
	var property	= jQuery.browser.msie && jQuery.browser.version < 7 ? 'height' : 'min-height';
	
	
	els.each(function(i, el_){
		t = jQuery(el_);
		
		var pvalue = height- parseInt(t.css('padding-top')) - parseInt(t.css('padding-bottom'));
		
		if(pvalue > 0){
			t.css(property, pvalue);
		}
	});
}